﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Dal.Shared;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;


namespace Gx.Bll.Manage
{
    public class LKWarehouseAdjustBll
    {
    
        protected LKWarehouseAdjustDal dal = new LKWarehouseAdjustDal();
        protected BaseDal<LKWarehouseAdjustDetail> detailDal = new BaseDal<LKWarehouseAdjustDetail>();
        protected BaseDal<LKWarehouseAdjustCost> costDal = new BaseDal<LKWarehouseAdjustCost>();

        protected LKAttachmentBll attachmentBll = new LKAttachmentBll();

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedList<LKWarehouseAdjust> GetLKWarehouseAdjustPageList(LKWarehouseAdjustInput input)
        {
            return dal.GetLKWarehouseAdjustPageList(input);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<LKWarehouseAdjust> GetLKWarehouseAdjusts()
        {
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增商品调仓单
        /// </summary>
        /// <param name="lKWarehouseAdjust"></param>
        /// <returns></returns>
        public ReturnModel AddLKWarehouseAdjust(LKWarehouseAdjustDto lKWarehouseAdjustDto)
        {
            var lKWarehouseAdjust= lKWarehouseAdjustDto.WarehouseAdjust;

            string maxCode = dal.GetMaxLKWarehouseAdjustCode();
            string code = "D" + DateTime.Now.ToString("yyMMdd") + "001";
            if (!string.IsNullOrWhiteSpace(maxCode))
            {
                var maxMonth = maxCode.Substring(2, 2);
                var nowMonth = DateTime.Now.ToString("MM");
                if (maxMonth == nowMonth)
                {
                    int num = Convert.ToInt32(maxCode);//Convert.ToInt32(maxCode.Substring(7, 3));
                    num++;
                    code = "D" + num.ToString();//DateTime.Now.ToString("yyMMdd") + num.ToString().PadLeft(3, '0');
                }
            }

            lKWarehouseAdjust.BillNo = code;


            lKWarehouseAdjust.CreateTime = DateTime.Now;
            lKWarehouseAdjust.CreateUserId = LoginHelper.GetUserId();
            lKWarehouseAdjust.CreateUser = LoginHelper.UserName();
            lKWarehouseAdjust.CompanyId = UserContext.CompanyId;
            var result = dal.AddLKWarehouseAdjust(lKWarehouseAdjustDto);//dal.Insert(lKWarehouseAdjust);

            if (result && lKWarehouseAdjustDto.AttachmentId > 0)
            {
                attachmentBll.SaveLKAttachment(new LKAttachment
                {
                    Id = lKWarehouseAdjustDto.AttachmentId,
                    BillNo = code,
                    BillType = (int)BillTypeTemplate.WarehouseOut,
                });
            }
            return new ReturnModel { Success = result, Msg = result ? "新增成功" : "新增失败" };
        }

        /// <summary>
        /// 编辑商品调仓单
        /// </summary>
        /// <param name="lKWarehouseAdjust"></param>
        /// <returns></returns>
        public bool EditLKWarehouseAdjust(LKWarehouseAdjustDto lKWarehouseAdjustDto)
        {
            var lKWarehouseAdjust = lKWarehouseAdjustDto.WarehouseAdjust;
            var cus = dal.Get(lKWarehouseAdjust.Id);
            if (cus == null)
            {
                return false;
            }
            if (cus.CompanyId != UserContext.CompanyId) return false;

            lKWarehouseAdjust.CreateTime = cus.CreateTime;
            lKWarehouseAdjust.CreateUserId = cus.CreateUserId;
            lKWarehouseAdjust.CreateUser = cus.CreateUser;
            lKWarehouseAdjust.ModifyUserId = LoginHelper.GetUserId();
            lKWarehouseAdjust.ModifyUser = LoginHelper.UserName();
            lKWarehouseAdjust.ModifyTime = DateTime.Now;
            lKWarehouseAdjust.CompanyId = UserContext.CompanyId;
            var result= dal.EditLKWarehouseAdjust(lKWarehouseAdjustDto);//dal.Update(lKWarehouseAdjust);

            if (result && lKWarehouseAdjustDto.AttachmentId > 0)
            {
                attachmentBll.DeleteLKAttachment(lKWarehouseAdjustDto.WarehouseAdjust.BillNo);
                attachmentBll.SaveLKAttachment(new LKAttachment
                {
                    Id = lKWarehouseAdjustDto.AttachmentId,
                    BillNo = lKWarehouseAdjustDto.WarehouseAdjust.BillNo,
                    BillType = (int)BillTypeTemplate.WarehouseAdjust,
                });
            }
            return result;

        }

        /// <summary>
        /// 删除商品调仓单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKWarehouseAdjust(int id)
        {
            return dal.DeleteLKWarehouseAdjust(id);//DeleteById(id);
        }
        
        /// <summary>
        /// 获取商品调仓单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LKWarehouseAdjustDto GetLKWarehouseAdjustById(int id)
        {
            var bill = dal.Get(id);

            if (bill.CompanyId != UserContext.CompanyId)
                return new LKWarehouseAdjustDto();

            var detailList = detailDal.GetListByWhere("AdjustId=@id", new { id }).ToList();
            var costList = costDal.GetListByWhere("AdjustId=@id", new { id }).ToList();

            var attachment = attachmentBll.GetLKAttachmentView(bill.BillNo);
            return new LKWarehouseAdjustDto { WarehouseAdjust = bill, Details = detailList, Costs = costList, FilePath = attachment?.AttachmentName, FileName = attachment?.OriginalName };
        }
    }
}

